package com.other.printer;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.Socket;

/**
 * ESC/POS 协议 热敏打印机
 * <p>
 * Android进阶——安卓调用ESC/POS打印机打印 - CSDN博客
 *
 * @link https://blog.csdn.net/haovip123/article/details/50739670
 * Created by ZeroVoid on 2018-7-30.
 */

public class EscPosUtil {
    //定义编码方式
    private static String encoding = null;

    private Socket sock = null;
    // 通过socket流进行读写
    private OutputStream socketOut = null;
    private OutputStreamWriter writer = null;

    private String CUT1 = "0x1D";
    private String CUT2 = "0x56";
    private String CUT3 = "0x00";
//    PAPER_PART_CUT = '\x1d\x56\x01'

    /**
     * 初始化Pos实例
     *
     * @param ip       打印机IP
     * @param port     打印机端口号
     * @param encoding 编码
     * @throws IOException
     */
    public EscPosUtil(String ip, int port, String encoding) throws IOException {
        sock = new Socket(ip, port);
        socketOut = new DataOutputStream(sock.getOutputStream());
        this.encoding = encoding;
        writer = new OutputStreamWriter(socketOut, encoding);
    }

    /**
     * 初始化打印机
     *
     * @throws IOException
     */
    protected void initPos() throws IOException {
        writer.write(0x1B);
        writer.write(0x40);
        writer.flush();
    }


    /**
     * 进纸并全部切割
     *
     * @return
     * @throws IOException
     */
    protected void feedAndCut() throws IOException {
        writer.write(CUT1);
        writer.write(CUT2);
        writer.write(CUT3);
        writer.write(300);
        //切纸前走纸多少
//        writer.write(100);
        writer.flush();

        //另外一种切纸的方式
        //        byte[] bytes = {29, 86, 0};
        //        socketOut.write(bytes);
    }

    /**
     * 打印文字
     *
     * @param text
     * @throws IOException
     */
    protected void printText(String text) throws IOException {
        String s = text;
        byte[] content = s.getBytes("gbk");
        socketOut.write(content);
        socketOut.flush();
    }

    /**
     * 新起一行，打印文字
     *
     * @param text
     * @throws IOException
     */
    protected void printTextNewLine(String text) throws IOException {
        //换行
        writer.write("\n");
        writer.flush();
        printText(text);
    }

    /**
     * 绝对打印位置
     *
     * @throws IOException
     */
    protected void printLocation(int light, int weight) throws IOException {
        writer.write(0x1B);
        writer.write(0x24);
        writer.write(light);
        writer.write(weight);
        writer.flush();
    }

    protected void printZz() throws IOException {
        writer.write(0x0c);
        writer.flush();
    }

    /**
     * 打印换行
     *
     * @return length 需要打印的空行数
     * @throws IOException
     */
    protected void printLine(int lineNum) throws IOException {
        for (int i = 0; i < lineNum; i++) {
            writer.write("\n");
        }
        writer.flush();
    }

    /**
     * 打印换行(只换一行)
     *
     * @throws IOException
     */
    protected void printLine() throws IOException {
        printLine(1);
    }


    /**
     * 关闭IO流和Socket
     *
     * @throws IOException
     */
    protected void closeIOAndSocket() throws IOException {
        writer.close();
        socketOut.close();
        sock.close();
    }

    private byte[] getBytes(String code) {
        byte[] content = new byte[0];
        try {
            content = code.getBytes("gbk");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return content;
    }

}
